package com.android.fragmentdemo
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.fragmentdemo.adapters.LooperTipItemAdapter
import com.android.fragmentdemo.adapters.StudyLayoutManagerAdapter
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityLooperTipBinding
import com.android.fragmentdemo.databinding.ActivityStudyLayoutManagerBinding
import com.android.fragmentdemo.definervlayoutmanager.LooperLayoutManager3
import com.android.fragmentdemo.definervlayoutmanager.StudyLayoutManager2
import com.bumptech.glide.Glide
import java.lang.ref.WeakReference

class LooperTipActivity : BaseActivity<ActivityLooperTipBinding>() {

    private val mHandler by lazy {
        HHHandler(this@LooperTipActivity)
    }

    override fun getLayoutId(): Int = R.layout.activity_looper_tip

    override fun init(savedInstanceState: Bundle?) {
        val mAdapter = LooperTipItemAdapter()
        mAdapter.itemDataSet = DataFactoryIp.createStrDataSet().subList(0, 5)
        //Log.d("TAG","itemCount00:${mAdapter.itemDataSet?.size}")
        //Log.d("TAG","itemCount00:${mAdapter.itemDataSet?.get((mAdapter.itemDataSet?.size ?: 0) - 1)}")
        mBinding.rvLooperTip.apply {
            layoutManager = LooperLayoutManager3()
            adapter = mAdapter
        }

        mHandler.sendEmptyMessageDelayed(MSG_DO_SCROLL, 2000)

        Glide.with(this@LooperTipActivity).load(IMG_URL).into(mBinding.shapeImageView)

        Glide.with(this@LooperTipActivity).load(IMG_URL).into(mBinding.shapeImageView1)
    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null)
    }

    private fun doLooperScroll() {
        mBinding.rvLooperTip.smoothScrollBy(0, getDimensionValue(R.dimen.dp_30))
        mHandler.sendEmptyMessageDelayed(MSG_DO_SCROLL, 2000)
    }

    private class HHHandler(activity: LooperTipActivity) : Handler(Looper.getMainLooper()) {
        private val weakReference = WeakReference(activity)
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            val tempActivity = weakReference.get() ?: return
            when (msg.what) {
                MSG_DO_SCROLL -> {
                    tempActivity.doLooperScroll()
                }
            }
        }
    }

    companion object {
        const val MSG_DO_SCROLL = 0x01
        const val MINE_TAG = "LooperTipActivity"

        const val IMG_URL = "https://i-blog.csdnimg.cn/img_convert/9769720980b48d1221e12be09b00a8b2.png"
    }
}